import { tooltipLength } from '@/utils';
import { Tooltip } from 'antd';
import pageStyles from '@/common/pageLayout.less';

const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);
const ColumnsRenderFun = ({ text, isTooltip = false, textStyle = {}, event = undefined, record = {}, wordNum = 50 }: Record<string, any>) => {
  return (
    <>
      <Tooltip title={isTooltip ? tooltipLength(text, wordNum) : undefined}>
        <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={textStyle}>
          {text == null || text == undefined || text == '' ? '--' : text}
        </span>
      </Tooltip>
    </>
  );
};
const formatDecimal = (val: any) => {
  // 不存在的话就返回'--'
  if (!val) return '0.00';
  // 将输入值转为数字，以确保处理的是数值类型
  const num = Number(val);
  // 如果转换后的数值是NaN，则直接返回原始值
  if (isNaN(num)) return '0.00';
  // 使用toFixed方法确保小数点后有两位，然后再转回字符串
  return num.toFixed(2) + '';
};

export const getProjectTableColumns = (): Record<string, any>[] => {
  return [
    {
      title: '预算项目编号',
      dataIndex: 'projectCode',
      key: 'projectCode',
      visible: true,
      width: 140,
      sorter: (a: any, b: any) => getSorter(a?.projectCode, b?.projectCode),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '预算项目名称',
      dataIndex: 'projectName',
      key: 'projectName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.projectName, b?.projectName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '预算项目费用（元）',
      dataIndex: 'projectSum',
      key: 'projectSum',
      visible: true,
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.projectSum, b?.projectSum),
      render: (text: string) => formatDecimal(text),
    },
  ];
};
